package animal;

/*
 *  user:38917
 *  date:2025/5/6
 *  time:00:23
 */

public class Test {
    public static void main1(String[] args) {
        Animal dog = new Dog();
        dog.eat();
    }
    public static void func(Animal a) {
        a.eat();
    }
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        func(dog);
        func(cat);
    }
    public static Animal func(String value) {
        if (value.equals("cat")) {
            return new Cat();
        }else if (value.equals("dog")) {
            return new Dog();
        }else {
            return null;
        }
    }
    public static void main2(String[] args) {
        Animal animal = func("cat");
    }
    public static void main3(String[] args) {
        //向上转型
        Animal dog = new Dog();
        System.out.println(dog.age = 10);
        System.out.println(dog.name = "Dog");
        //向下转型
        /*Dog dog1 = (Dog) dog;
        System.out.println(dog1.color = "白色");
        //错误的向下转型
        Cat cat = (Cat) dog;
        cat.weight = 10;*/
        if (dog instanceof Dog) {
            //dog instanceof Dog 为true
            //说明该向下转型是安全的
            Dog dog1 = (Dog) dog;
        }
        if (dog instanceof Cat) {
            //dog instanceof Cat 为false
            //说明该向下转型是不安全的,不执行if语句中的代码
            Cat cat = (Cat) dog;
        }
    }
}
